package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @create: 2024/7/19
 * @author: gyhe10
 * @description: 后台管理系统维护会员信息控制层
 **/

@Api(tags = "后台管理系统会员接口管理")
@RestController
@RequestMapping("admin/user")
public class SysMemberController {

    @Resource
    private MemberService memberService;

    /**
     * 多条件分页查询会员列表
     *
     * @param current  页码
     * @param size     每页记录数
     * @param nickName 用户昵称
     * @param status   用户状态
     * @return
     */
    @ApiOperation("多条件分页查询会员列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:user:page')")
    public Result<Page<Member>> loadMemberPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) String nickName,
                                               @RequestParam(required = false) Integer status) {
        Page<Member> page = new Page<>(current, size);
        page = memberService.page(page, new LambdaQueryWrapper<Member>()
                .eq(ObjectUtil.isNotNull(status), Member::getStatus, status)
                .like(StringUtils.hasText(nickName), Member::getNickName, nickName)
                .orderByDesc(Member::getCreateTime));
        return Result.success(page);
    }

    /**
     * 根据ID查询会员详情
     *
     * @param memberId 会员ID
     * @return
     */
    @ApiOperation("根据ID查询会员详情")
    @GetMapping("info/{memberId}")
    @PreAuthorize("hasAuthority('admin:user:info')")
    public Result<Member> loadMemberInfo(@PathVariable Long memberId) {
        // Member member = memberService.getById(memberId);
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                // 仅查询部分字段
                .select(Member::getId, Member::getNickName, Member::getStatus, Member::getOpenId, Member::getPic)
                .eq(Member::getId, memberId)
                // 约定：会员状态为-1表示逻辑删除
                .in(Member::getStatus, 0, 1)
        );
        return Result.success(member);
    }

    /**
     * 修改会员状态
     *
     * @param member 会员对象
     * @return
     */
    @ApiOperation("修改会员状态")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:user:update')")
    public Result<String> modifyMemberStatus(@RequestBody Member member) {
        // 补充会员对象属性
        member.setUpdateTime(new Date());
        boolean modified = memberService.updateById(member);
        return Result.handle(modified);
    }

    /**
     * 单个/批量删除会员
     *
     * @param memberIds 会员ID集合
     * @return
     */
    @ApiOperation("单个/批量删除会员")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:user:delete')")
    public Result<String> removeMember(@RequestBody List<Integer> memberIds) {
        // 约定：会员状态为-1表示用户已删除
        // 创建会员对象集合
        List<Member> memberList = new ArrayList<>(memberIds.size());
        memberIds.forEach(id -> {
            Member member = new Member();
            member.setId(id);
            member.setStatus(-1);
            memberList.add(member);
        });
        boolean removed = memberService.updateBatchById(memberList);
        return Result.handle(removed);
    }

    //////////////// feign 接口 ////////////////

    /**
     * 根据openId查询会员昵称
     *
     * @param openId
     * @return
     */
    @GetMapping("getNickNameByOpenId")
    public Result<String> getNickNameByOpenId(@RequestParam String openId) {
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .select(Member::getNickName)
                .eq(Member::getOpenId, openId));
        return Result.success(member.getNickName());
    }

}
